﻿using System;
using System.Data.OleDb;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Text;
using System.Collections;


public partial class Crm_EditArticle : BaseCrm
{
    List<Dukey.Model.Boards> listboard = BLL.Boards.instance.GetList("");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {          
            dropSort.DataSource = listboard; 
            dropSort.DataBind();

            if (StringHelper.isNum(Request.QueryString["id"]))//如果是更新数据,则加载该记录.
            {
                int id = int.Parse(Request.QueryString["id"]);
                Dukey.Model.Articles model = BLL.Articles.instance.GetModel(id);
                if (model != null)
                {
                    txtTitle.Text = model.title;
                    txtBrief.Text = model.brief;
                    txtContent.Value = model.contents;
                    chkIsShow.Checked = model.isshow;
                    txtDots.Text = model.dots.ToString();
                    txtPublishTime.Text = model.addtime.ToString();
                    txtOrders.Text = model.px.ToString();
                    dropSort.SelectedValue = model.bid.ToString();
                    file1.Value = model.picurl;
                }
            }
            else
            {
                txtPublishTime.Text = DateTime.Now.ToString("yyyy-M-d");
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //上传文件
        string imageName = file1.Value.Trim();
        Dukey.Model.Articles model = new Dukey.Model.Articles();
        model.title = StringHelper.ReplaceBadChar(txtTitle.Text);
        model.brief = StringHelper.ReplaceBadChar(txtBrief.Text);
        model.contents = txtContent.Value;
        model.bid = int.Parse(dropSort.SelectedValue);
        model.isshow = chkIsShow.Checked;
        DateTime dt = DateTime.Now;
        DateTime.TryParse(txtPublishTime.Text, out dt);
        model.addtime = dt;
        model.picurl = imageName;
        int px = 0;
        int.TryParse(txtOrders.Text, out px);
        model.px = px;
        //if (!StringHelper.isNum(Request.QueryString["id"]))//插入新纪录
        //{
        //    BLL.Articles.instance.Add(model);
        //}
        //else//更新纪录
        //{
        //    model.id = int.Parse(Request.QueryString["id"]);
        //    BLL.Articles.instance.Update(model);
        //}
    
        Hashtable hs = new Hashtable();
        hs.Add("title", model.title);
        hs.Add("brief", model.brief);
        hs.Add("contents", model.contents);
        hs.Add("bid", model.bid);
        hs.Add("isshow", model.isshow);
        hs.Add("addtime", model.addtime);
        hs.Add("picurl", model.picurl);
        if (!StringHelper.isNum(Request.QueryString["id"]))//插入新纪录
        {
            BLL.DataBaseHelper.instance.Insert(hs, "Articles");  
            txtTitle.Text = "";
        txtBrief.Text = "";
        txtContent.Value = "";
        Common.MessageBox.Show(this, "发布成功,你可以继续发布下一篇!");
        }
        else//更新纪录
        {
            model.id = int.Parse(Request.QueryString["id"]);            
            BLL.DataBaseHelper.instance.Update(hs, "Articles", "[id]=" + model.id);
            Common.MessageBox.ShowAndRedirect(this, "修改成功!","ArtilceList.aspx");
        }        
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        BLL.DataBaseHelper.instance.Delete("Articles", "id=" + Request.QueryString["id"]);
        Response.Redirect("articlelist.aspx");
    }

    [System.Web.Services.WebMethod(true)]
    public static void SubmitForm()
    {

    }
}
